package com.pat.jrgwt.client;

import java.util.Date;
import java.util.HashMap;

import com.google.gwt.core.client.GWT;

public class ProfitLossesHolder
{
    
    /**
     * Holds profit/losses values (Forex) for specified year
     */
    protected HashMap<String, double[]> m_arValuesFx = new HashMap<String, double[]>();

    /**
     * Holds profit/losses values (Futures) for specified year
     */
    protected HashMap<String, double[]> m_arValuesFutures = new HashMap<String, double[]>();

    @SuppressWarnings("deprecation")
    protected int m_yearInUse = new Date().getYear();
    
    /**
     * Holds profit/losses values (Forex)
     * 
     * @param year
     * @param data
     */
    public void setDataFx(int year, HashMap<String, double[]> data)
    {
        m_yearInUse = year;
        m_arValuesFx = data;
//        ProfitLossesView.getInstance().recalculate();
    }
    
    /**
     * Holds profit/losses values (Futures)
     */
    public void setDataFutures(int year, HashMap<String, double[]> data)
    {
        m_yearInUse = year;
        m_arValuesFutures = data;
    }
    
    public String[] getExchangeNames()
    {
        return new String[] { "EURUSD-30", "AUDJPY-120", "USDJPY-60", "USDJPY-120", "GBPJPY-60", "EURNZD-120" };
//        return (String[]) m_arValues.keySet().toArray(new String[m_arValues.size()]);
    }
    
    public String[] getExchangeNamesFutures()
    {
        if( m_arValuesFutures.isEmpty() )
        {
            return null;
        }
        
        return m_arValuesFutures.keySet().toArray(new String[m_arValuesFutures.size()]);
    }
    
    public double get(String exchangeName, int month)
    {
        return get(exchangeName, month, m_yearInUse);
    }
    
    /**
     * Gets value of exchange for specific year and month
     * 
     * @param exchangeName
     * @param month
     * @param year
     * @return
     */
    public double get(String exchangeName, int month, int year)
    {
        double v = Double.MIN_VALUE;
        
//        GWT.log("Getting P/L for " + exchangeName + ", " + year + "-" + month);
        if( year == m_yearInUse )
        {
            if( m_arValuesFx.containsKey(exchangeName))
            {
                if( (month > 0) && (month < 13) )
                {
                    v = m_arValuesFx.get(exchangeName)[month - 1];
                }else
                {
                    GWT.log("Illegal month: " + month);
                }
            }else
            {
                GWT.log("Year " + year + " has no exchange name: " + exchangeName);
            }
        }else
        {
            GWT.log("No profit/losses for year " + year);
        }
//        GWT.log("value: " + v);
        
        return v;
    }

    public double getFutures(String exchangeName, int month)
    {
        return getFutures(exchangeName, month, m_yearInUse);
    }

    /**
     * Gets value of exchange for specific year and month
     * 
     * @param exchangeName
     * @param month
     * @param year
     * @return
     */
    public double getFutures(String exchangeName, int month, int year)
    {
        double v = Double.MIN_VALUE;
        
//        GWT.log("Getting P/L for " + exchangeName + ", " + year + "-" + month);
        if( year == m_yearInUse )
        {
            if( m_arValuesFutures.containsKey(exchangeName))
            {
                if( (month > 0) && (month < 13) )
                {
                    v = m_arValuesFutures.get(exchangeName)[month - 1];
                }else
                {
                    GWT.log("Illegal month: " + month);
                }
            }else
            {
                GWT.log("Year " + year + " has no exchange name: " + exchangeName);
            }
        }else
        {
            GWT.log("No profit/losses for year " + year);
        }
//        GWT.log("value: " + v);
        
        return v;
    }

    
}
